//
//  BottleSettingController.m
//  Water
//
//  Created by yh on 2018/1/15.
//  Copyright © 2018年 chenguibang. All rights reserved.
//

#import "BottleSettingController.h"
#import "BottleDetailController.h"
#import <BlocksKit/BlocksKit+UIKit.h>
#import <YYKit/UITableView+YYAdd.h>
@interface BottleSettingController ()<UITableViewDelegate,UITableViewDataSource>

@end

@implementation BottleSettingController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"UITableViewCell"];
     [self setEditbuttonWithstyle:UIBarButtonSystemItemEdit];
}


- (void)setEditbuttonWithstyle:(UIBarButtonSystemItem)style{
    UIBarButtonItem *editBtn = [[UIBarButtonItem alloc]bk_initWithBarButtonSystemItem:style handler:^(id sender) {
        [self.tableView setEditing:style == UIBarButtonSystemItemEdit animated:YES];
        [self setEditbuttonWithstyle:style == UIBarButtonSystemItemEdit ? UIBarButtonSystemItemDone : UIBarButtonSystemItemEdit];
    }];
    
    UIBarButtonItem *addBtn = [[UIBarButtonItem alloc]bk_initWithBarButtonSystemItem:UIBarButtonSystemItemAdd handler:^(id sender) {
        BottleDetailController *bottleDetailController = [[BottleDetailController alloc]initWithNibName:@"BottleDetailController" bundle:nil];
        bottleDetailController.title = @"水杯详情";
        bottleDetailController.save = ^(BottleItem *bottle) {
            [[AppClient shared] addBottle:bottle];
            [[AppClient shared] saveBottles];
            [self.tableView reloadData];
        };
        [self.navigationController pushViewController:bottleDetailController animated:YES];
    }];
    
    self.navigationItem.rightBarButtonItems = @[addBtn,editBtn];
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        BottleItem *item = [AppClient shared].customBottles[indexPath.row];
        [[AppClient shared] deleteBottle:item];
        [[AppClient shared] saveBottles];
        [tableView deleteRowAtIndexPath:indexPath withRowAnimation:UITableViewRowAnimationFade];
    }
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [AppClient shared].customBottles.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"UITableViewCell" forIndexPath:indexPath];
    BottleItem *item = [AppClient shared].customBottles[indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@",item.amount];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    BottleItem *item = [AppClient shared].customBottles[indexPath.row];
    BottleDetailController *bottleDetailController = [[BottleDetailController alloc]initWithNibName:@"BottleDetailController" bundle:nil];
    bottleDetailController.title = @"水杯详情";
    bottleDetailController.bottle = item;
    bottleDetailController.save = ^(BottleItem *bottle) {
        [[AppClient shared] addBottle:bottle];
        [[AppClient shared] saveBottles];
        [self.tableView reloadData];
    };
    [self.navigationController pushViewController:bottleDetailController animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
